package com.icezzz.db_valid;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.sql.DataSource;

import cn.hutool.core.io.FileUtil;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.meta.Column;
import cn.hutool.db.meta.MetaUtil;
import cn.hutool.db.meta.Table;

/**
 * Hello world!
 *
 */
public class Main {
    public static void main(String[] args) {
        String file = "C:\\Users\\Administrator\\Desktop\\database.txt";
        DataSource dataSource = DSFactory.get();
        StringBuilder sb = new StringBuilder();
        List<String> tables = MetaUtil.getTables(dataSource);
        for (int i = 0; i < tables.size(); i++) {
            String tableName = tables.get(i);
            Table tableMeta = MetaUtil.getTableMeta(dataSource, tableName);
            String comment = tableMeta.getComment();
            sb.append("==============================\r\n");
            sb.append("" + comment + ">>" + tableName + "\r\n");
            Collection<Column> columns = tableMeta.getColumns();
            Iterator<Column> iterator = columns.iterator();
            while (iterator.hasNext()) {
                Column column = iterator.next();
                String name = column.getName();
                String typeName = column.getTypeName();
                String columnComment = column.getComment();
//                int type = column.getType();
//                JdbcType typeEnum = column.getTypeEnum();
                Long lengthLong = column.getSize();
                int length = lengthLong.intValue();
                columnComment = StringUtils.removeLineChar(columnComment);
                sb.append("   " + name + ">" + typeName + ">" + columnComment + "\r\n");
                String valid = ValidUtils.valid(tableName, name, typeName, columnComment, length);
                if (valid != null) {
                    System.out.println(valid);
                }
            }
        }
        String content = sb.toString();
        FileUtil.writeUtf8String(content, file);
    }
}
